﻿using CemsTransfer.Event;
using CemsTransfer.Models;

namespace CemsTransfer.BUS
{
    public class CmdListServices : BackgroundService
    {
        private Timer _timer;

        private readonly IServiceScopeFactory _serviceScopeFactory;

        public CmdListServices(IServiceScopeFactory serviceScopeFactory)
        {
            _serviceScopeFactory = serviceScopeFactory;
        }

        /// <summary>
        /// 定时服务
        /// </summary>
        /// <param name="stoppingToken"></param>
        /// <returns></returns>
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            int interval = Convert.ToInt32(AppSettingsHelper.Configuration["Interval"]);
            _timer = new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromMilliseconds(interval));

            await Task.CompletedTask;
        }

        /// <summary>
        /// 要调用的方法
        /// </summary>
        /// <param name="state"></param>
        private async void DoWork(object state)
        {
            List<CmdInfo> cmdInfos = CemsDataProcess.cmdInfos;
            if (cmdInfos.Count > 0)
            {
                CemsDataProcess.SendToSerial(cmdInfos[0].cmd);
                CemsDataProcess.cmdInfos.RemoveAt(0);
            }
            else
            {
                if (CemsDataProcess.deviceInfos != null && CemsDataProcess.deviceInfos.Count > 0)
                {
                    int index = CemsDataProcess.deviceInfos.FindIndex(x => x.flag == 1);
                    if (index > -1)
                    {
                        DeviceInfo deviceInfo = CemsDataProcess.deviceInfos[index];
                        CemsDataProcess.SendToSerial(deviceInfo.querycmd);
                        if (index + 1 < CemsDataProcess.deviceInfos.Count)
                        {
                            deviceInfo.flag = 0;
                            CemsDataProcess.deviceInfos[index] = deviceInfo;
                            CemsDataProcess.deviceInfos[index + 1].flag = 1;
                        }
                        else if (index + 1 == CemsDataProcess.deviceInfos.Count)
                        {
                            deviceInfo.flag = 0;
                            CemsDataProcess.deviceInfos[index] = deviceInfo;
                            CemsDataProcess.deviceInfos[0].flag = 1;
                        }
                    }
                    else
                    {
                        DeviceInfo deviceInfo = CemsDataProcess.deviceInfos[0];
                        CemsDataProcess.SendToSerial(deviceInfo.querycmd);
                        if (CemsDataProcess.deviceInfos.Count > 1)
                        {
                            CemsDataProcess.deviceInfos[1].flag = 1;
                        }
                        else
                        {
                            CemsDataProcess.deviceInfos[0].flag = 1;
                        }
                    }

                }
            }
        }
    }
}
